package doorwifi.wd.patientsation.peripheral.mainapp.main;


import doorwifi.wd.patientsation.peripheral.mainapp.R;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.Broadcast;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.CallDoctor;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.CallDoor;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.CallSickbed;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.CallSite;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.Trusteeship;
import doorwifi.wd.patientsation.peripheral.mainapp.fragment.systemSetting;
import doorwifi.wd.patientsation.peripheral.mainapp.utils.UIUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends Activity implements OnClickListener{

//    private String[] AryTxt = { "呼叫病床", "呼叫门口", "呼叫医生", "呼叫站点", "广播","托管","系统设置" };
    private LayoutInflater inflater;
    @ViewInject(R.id.xh_call_sickbed)
    private LinearLayout xh_call_sickbed;
    @ViewInject(R.id.xh_call_door)
    private LinearLayout xh_call_door;
    @ViewInject(R.id.xh_call_doctor)
    private LinearLayout xh_call_doctor;
    @ViewInject(R.id.xh_call_site)
    private LinearLayout xh_call_site;
    @ViewInject(R.id.xh_broadcast)
    private LinearLayout xh_broadcast;
    @ViewInject(R.id.xh_trusteeship)
    private LinearLayout xh_trusteeship;
    @ViewInject(R.id.xh_system_setting)
    private LinearLayout xh_system_setting;



    // 选项卡对应相应的fragment
//    private Class[] fragment = { CallSickbed.class, CallDoor.class, CallDoctor.class, CallSite.class, Broadcast.class , Trusteeship.class,systemSetting.class};

    private CallSickbed callSickbed;
    private CallDoor CallDoor;
    private CallDoctor CallDoctor;
    private CallSite CallSite;
    private Broadcast Broadcast;
    private Trusteeship Trusteeship;
    private systemSetting systemSetting;
        private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        setContentView(R.layout.activity_main);
        x.view().inject(this);
        initOnclick();
        fragmentManager = getFragmentManager();
        setTabSelection(0);

    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     */
    private void setTabSelection(int index) {
        resetBtn();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况  45b9f2
        hideFragments(transaction);
        switch (index) {
            case 0:
                xh_call_sickbed.setBackgroundColor(Color.GRAY);
                if (callSickbed == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    callSickbed = new CallSickbed();
                    transaction.add(R.id.realtabcontent,callSickbed);

                } else {

                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(callSickbed);
                }
                break;
            case 1:
                xh_call_door.setBackgroundColor(Color.GRAY);
                if (CallDoor == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    CallDoor = new CallDoor();
                    transaction.add(R.id.realtabcontent  , CallDoor);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(CallDoor);
                }
                break;
            case 2:
                xh_call_doctor.setBackgroundColor(Color.GRAY);
                if (CallDoctor == null) {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    CallDoctor = new CallDoctor();
                    transaction.add(R.id.realtabcontent  , CallDoctor);
                } else {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(CallDoctor);
                }
                break;
            case 3:
                xh_call_site.setBackgroundColor(Color.GRAY);
                // 当点击了设置tab时，改变控件的图片和文字颜色
                if (CallSite == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    CallSite = new CallSite();
                    transaction.add(R.id.realtabcontent  , CallSite);
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(CallSite);
                }
                break;
            case 4:
                xh_broadcast.setBackgroundColor(Color.GRAY);
                if (Broadcast == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    Broadcast = new Broadcast();
                    transaction.add(R.id.realtabcontent  , Broadcast);
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(Broadcast);
                }
                break;
            case 5:
                xh_trusteeship.setBackgroundColor(Color.GRAY);
                if (Trusteeship == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    Trusteeship = new Trusteeship();
                   transaction.add(R.id.realtabcontent  , Trusteeship);
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(Trusteeship);
                }
                break;
            case 6:
                xh_system_setting.setBackgroundColor(Color.GRAY);
                if (systemSetting == null) {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    systemSetting = new systemSetting();
                  transaction.add(R.id.realtabcontent  , systemSetting);
                } else {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(systemSetting);
                }
                break;
        }


        transaction.commit();
    }

    private void resetBtn() {
        xh_call_sickbed.setBackgroundColor(Color.WHITE);
        xh_call_door.setBackgroundColor(Color.WHITE);
        xh_call_doctor.setBackgroundColor(Color.WHITE);
        xh_call_site.setBackgroundColor(Color.WHITE);
        xh_broadcast.setBackgroundColor(Color.WHITE);
        xh_trusteeship.setBackgroundColor(Color.WHITE);
        xh_system_setting.setBackgroundColor(Color.WHITE);
    }

    private void initOnclick() {
        xh_call_sickbed.setOnClickListener(this);
        xh_call_door.setOnClickListener(this);
        xh_call_doctor.setOnClickListener(this);
        xh_call_site.setOnClickListener(this);
        xh_broadcast.setOnClickListener(this);
        xh_trusteeship.setOnClickListener(this);
        xh_system_setting.setOnClickListener(this);
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     *
     * @param //transaction
     *            用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
//        CallSickbed.class, CallDoor.class, CallDoctor.class, CallSite.class, Broadcast.class , Trusteeship.class,systemSetting.class
        if (callSickbed != null) {
            transaction.hide(callSickbed);
        }
        if (CallDoor != null) {
            transaction.hide(CallDoor);
        }
        if (CallDoctor != null) {
            transaction.hide(CallDoctor);
        }
        if (CallSite != null) {
            transaction.hide(CallSite);
        }
        if (Broadcast != null) {
            transaction.hide(Broadcast);
        }
        if (Trusteeship != null) {
            transaction.hide(Trusteeship);
        }
        if (systemSetting != null) {
            transaction.hide(systemSetting);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.xh_call_sickbed:
                setTabSelection(0);
                break;
            case R.id.xh_call_door:
                setTabSelection(1);
                break;
            case R.id.xh_call_doctor:
                setTabSelection(2);
                break;
            case R.id.xh_call_site:
                setTabSelection(3);
                break;
            case R.id.xh_broadcast:
                setTabSelection(4);
                break;
            case R.id.xh_trusteeship:
                setTabSelection(5);
                break;
            case R.id.xh_system_setting:
                setTabSelection(6);
                break;
        }

    }
}
